package com.navidove.web.manager.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.navidove.appspotlight.model.bo.AppDictionary;
import com.navidove.appspotlight.model.bo.AppItem;
import com.navidove.appspotlight.model.service.AppDictionaryService;
import com.navidove.appspotlight.model.service.AppItemService;
import com.navidove.web.manager.ctrl.AppAdItemQueryCtrl.Query;
import com.navidove.web.vo.JqGridPaging;

@Controller
@RequestMapping("/app/appItemPicker")
public class AppItemPickerCtrl  {
    
	@Autowired
	private AppItemService appItemService;
	
	@Autowired
	private AppDictionaryService appDictionaryService;
	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
    }
    
    @ModelAttribute
    public void reset(Model model) {        
        JqGridPaging paging = new JqGridPaging();
        model.addAttribute("paging",paging);
        
        model.addAttribute("query",new Query());
    }
    
    
    @RequestMapping("/init")
    public String init(Model model) {
        List<AppDictionary> sourceList = appDictionaryService.getAppDictionaryList(AppDictionary.DICTIONARY_TYPE_SOURCE);
        List<AppDictionary> categoryList = appDictionaryService.getAppDictionaryList(AppDictionary.DICTIONARY_TYPE_CATEGORY);
        
        model.addAttribute("sourceList", sourceList);
        model.addAttribute("categoryList", categoryList);
        
        return "/app/appItem_picker";
    }
    
    @ResponseBody
    @RequestMapping("/query")
    public Map<String, Object> query(Model model, 
            @ModelAttribute("query") Query query,
            @ModelAttribute("paging") JqGridPaging paging) {
    	
    	Map<String, Object> result=new HashMap<String, Object>();
    	List<Map<String, Object>> rowsList=new ArrayList<Map<String, Object>>();
    	List<AppItem> list = appItemService.getAppItemList(query.getAppName(), query.getOwner(), query.getSourceId(), query.getCategoryId(), Boolean.TRUE, paging);
    	for (AppItem appItem : list) {
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put("id", appItem.getId());
    		map.put("appName", appItem.getAppName());
    		map.put("appSource", appItem.getAppSource().getDictionaryName());
    		map.put("appCategory", appItem.getAppCategory().getDictionaryName());
    		map.put("appIntro", appItem.getAppIntro());
    		map.put("owner", appItem.getOwner());
    		rowsList.add(map);
    	}
    	
    	result = paging.convert(rowsList);
        return result;  
    }  
}
